package com.doctor.utils.network;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.doctor.utils.callback.SimpleCallback;
import com.doctor.utils.logutils.LogUtils;

/**
 * Created by chemin on 2016/7/2.
 */
public class GetImageUtil {
    public static void getImageView(Context context, String urlString, ImageView imageView, int placeholderId, int errorResId) {
        if (urlString != null) {
//            Picasso.with(context).load(Uri.parse(urlString)).config(Bitmap.Config.RGB_565).placeholder(placeholderId).error(errorResId).into(imageView);
            Glide.with(context).load(Uri.parse(urlString)).asBitmap().fitCenter().skipMemoryCache(true).error(errorResId).placeholder(placeholderId).into(imageView);
        }
    }

    public static void getImageViewScaleHeight(Context context, String urlString, ImageView imageView, int placeholderId, int errorResId) {
        if (urlString != null) {
//            Picasso.with(context).load(Uri.parse(urlString)).config(Bitmap.Config.RGB_565).placeholder(placeholderId).error(errorResId).transform(new ScaleAccordingToWidthTrans(imageView)).into(imageView);
            Glide.with(context).load(Uri.parse(urlString)).asBitmap().fitCenter().skipMemoryCache(true).error(errorResId).placeholder(placeholderId).into(imageView);
        }
    }

    public static void getImageViewMemoryCache(Context context, String urlString, ImageView imageView) {
        if (urlString != null) {
//            Picasso.with(context).load(Uri.parse(urlString)).config(Bitmap.Config.RGB_565).placeholder(placeholderId).error(errorResId).into(imageView);
//            Glide.with(context).load(Uri.parse(urlString)).asBitmap().placeholder(R.mipmap.zhangwei).error(R.mipmap.zhangwei).fitCenter().into(imageView);
        }
    }

    public static void getBannerImageView(Context context, String urlString, ImageView imageView, int placeholderId, int errorResId) {
        if (urlString != null) {
            Glide.with(context).load(Uri.parse(urlString)).asBitmap().skipMemoryCache(true).error(errorResId)
                   .placeholder(placeholderId).into(imageView);
        }
    }



    public static void getImageGlideNoPlaceWithNoCache(Context context, String urlString, ImageView imageView) {
        if (urlString != null) {
//            LogUtils.i("=====glide加载");
            Glide.clear(imageView);
            Glide.get(context).clearMemory();
//            Glide.get(context).setMemoryCategory(MemoryCategory.LOW);
            Glide.with(context).load(Uri.parse(urlString)).asBitmap().
                    centerCrop().format(DecodeFormat.PREFER_RGB_565).
                    skipMemoryCache(true).into(imageView);
        }
    }



    public static void getBitmap(Context context, String url, final SimpleCallback<Bitmap> simpleCallback) {
        if (!TextUtils.isEmpty(url)) {
            Glide.with(context).load(url).asBitmap().into(new SimpleTarget<Bitmap>() {
                @Override
                public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
                    simpleCallback.onResult(true,resource);
                }
            }); //方法中设置asBitmap可以设置回调类型
        }else {
            LogUtils.e("thumbUrl====null");
        }
    }

}
